package com.tqz.dt.business.service;

import com.tqz.dt.anno.GlobalTransactional;
import com.tqz.dt.business.dao.AccountInfoDao;
import com.tqz.dt.business.dao.ProductInfoDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * <p>支付实现类
 *
 * @author tianqingzhao
 * @since 2021/7/7 16:22
 */
@Service
public class PayServiceImpl implements PayService {

    @Autowired
    private AccountInfoDao accountInfoDao;

    @Autowired
    private ProductInfoDao productInfoDao;

    //    @Transactional(rollbackFor = Exception.class)
    @GlobalTransactional
    @Override
    public void pay(Integer accountId, Integer productId, double money) {
        //查询余额
        double blance = accountInfoDao.qryBlanceByUserId(accountId);

        updateProduct(blance, productId, money);

        //更新余额
        int retVal = accountInfoDao.updateAccountBlance(accountId, money);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateProduct(double blance, Integer productId, double money) {
        int i = productInfoDao.updateProductInfo(productId);

        //余额不足正常逻辑
        if (new BigDecimal(blance).compareTo(new BigDecimal(money)) < 0) {
            throw new RuntimeException("余额不足");
        }
        int j = 1 / 0;
    }
}
